/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fx.beer;

import beans.Beer;
import dao.DaoBeer;
import fx.Home;
import fx.avaliation.AvaliationController;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javax.swing.JOptionPane;

/**
 * FXML Controller class
 *
 * @author Jandrei
 */
public class BeerChooseController implements Initializable {

    Home application;
    List<Beer> list;
    DaoBeer dao;

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<Beer> tableBeer;

    @FXML
    private TableColumn<Beer, String> columnId;

    @FXML
    private TableColumn<Beer, String> columnMark;

    @FXML
    private TableColumn<Beer, String> columnName;

    @FXML
    void btnSelect(ActionEvent event) {
        Long id = tableBeer.getSelectionModel().getSelectedItem().getId();
        Beer beer = dao.getById(id);
//        application.setBeerSelected(beer);
//        JOptionPane.showMessageDialog(null, beer.getName());
         try {
            AvaliationController avaliation = new AvaliationController();
            avaliation = (AvaliationController) application.replaceSceneContent("avaliation/Avaliation.fxml", Home.AVALIATIONWIDTH, Home.AVALIATIONHEIGTH, "Evaluation of beer " + beer.getName());
            avaliation.setBeer(beer);
            avaliation.setApp(application);
            
        } catch (Exception ex) {
            Logger.getLogger(BeerChooseController.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    @FXML
    void initialize() {
        assert columnId != null : "fx:id=\"columnId\" was not injected: check your FXML file 'beerChoose.fxml'.";
        assert columnMark != null : "fx:id=\"columnMark\" was not injected: check your FXML file 'beerChoose.fxml'.";
        assert columnName != null : "fx:id=\"columnName\" was not injected: check your FXML file 'beerChoose.fxml'.";
        assert tableBeer != null : "fx:id=\"tableBeer\" was not injected: check your FXML file 'beerChoose.fxml'.";

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        dao = new DaoBeer();
        list = dao.getAll();
        final ObservableList<Beer> data = FXCollections.observableArrayList();
        for (Beer b : list) {
            Beer be = new Beer();
            be.setId(b.getId());
            be.setName(b.getName());
            be.setMark(b.getMark());
            data.add(be);
        }
        columnId.setCellValueFactory(new PropertyValueFactory<Beer,String>("Id"));
        columnName.setCellValueFactory(new PropertyValueFactory<Beer,String>("Name"));
        columnMark.setCellValueFactory(new PropertyValueFactory<Beer,String>("Mark"));
        tableBeer.setItems(data);

    }

    public void setApp(Home application) {
        this.application = application;
    }

}
